package com.higgins.eventtracking.server;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.AssetDTO;
import com.higgins.eventtracking.client.dto.FinanceGoalDTO;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class AssetTrackingServiceImpl extends RemoteServiceServlet implements AssetTrackingService {
	private static final Logger log = Logger.getLogger(AssetTrackingServiceImpl.class.getName());

	public String addAsset(String amount, String account) throws IllegalArgumentException {
		AssetTrackingDataService dataService = new AssetTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			dataService.addAsset(amount, account);
			return "Update was successful for addAsset (" +  amount + ", " + account + ").\n";
		} catch (Exception e)	{
			return "Update was unsuccessful for addAsset.\n";
		} finally	{
			dataService.closePersistenceManager();
		}
	}
	public String addAsset(String amount, String account, String accountRepayment, String monthlyRepayment, String monthlyInterest) throws IllegalArgumentException {
		AssetTrackingDataService dataService = new AssetTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			dataService.addAsset(amount, account, accountRepayment, monthlyRepayment, monthlyInterest);
			return "Update was successful for addAsset (" +  amount + ", " + account + ", " + accountRepayment  + ", " + monthlyRepayment  + ", " + monthlyInterest + ").\n";
		} catch (Exception e)	{
			return "Update was unsuccessful for addAsset.\n";
		} finally	{
			dataService.closePersistenceManager();
		}
	}
	
	public String retrieveTotalAssetAmount()	{
		AssetTrackingDataService dataService = new AssetTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			return dataService.retrieveAssetAmount(null).toString();
		} catch (Exception e)	{
			return "Update was unsuccessful for retrieveTotalAssetAmount.\n";
		} finally	{
			dataService.closePersistenceManager();
		}
	}
	public String retrieveForecastedEndOfNextQuarterTotalAssets(Boolean employed, Date now)	throws	IllegalArgumentException	{
		Date endOfNextQuarter = DateUtility.getEndOfNextQuarter(now);
		return retrieveForecastedTotalAssets(employed, now, endOfNextQuarter);
	}
	public String retrieveForecastedTotalAssets(Boolean employed, Date now, Date forecastDate)	throws IllegalArgumentException	{
		AssetTrackingDataService assetService = new AssetTrackingDataService();
		try	{
			assetService.openPersistenceManager();
			return assetService.retrieveForecastedTotalAssets(employed, now, forecastDate).toString();
		} catch (Exception e)	{
			return "Update was unsuccessful for retrieveForecastedEndOfYearTotalAssets.\n";
		} finally	{
			assetService.closePersistenceManager();
		}
	}
/*	public String retrieveForecastedUnemployedEndOfNextQuarterTotalAssets(Date now)	throws	IllegalArgumentException	{
		AssetTrackingDataService assetService = new AssetTrackingDataService();
		try	{
			assetService.openPersistenceManager();
			Double totalAssets = assetService.retrieveAssetAmount(null);
			
			Date endOfNextQuarter = DateUtility.getEndOfNextQuarter(now);
			Double profitToEndOfNextQuarter = calculateForecastProfit(false, now, endOfNextQuarter);
			
			Double forecast = totalAssets + profitToEndOfNextQuarter;
			return forecast.toString();
		} catch (Exception e)	{
			return "Update was unsuccessful for retrieveForecastedUnemployedEndOfYearTotalAssets.\n";
		} finally	{
			assetService.closePersistenceManager();
		}
	}	*/
	
	public String retrieveEndOfNextQuarter(Date now)	{
		Date endOfNextQuarter = DateUtility.getEndOfNextQuarter(now);
		return DateUtility.getMonth(DateUtility.getMonth(endOfNextQuarter)) + " " 
				+ DateUtility.getYear(endOfNextQuarter);
	}
	
	@Deprecated
	public Double calculateAverageMonthlyProfit(boolean employed)	{
		IncomeSettingsDataService incomeService = new IncomeSettingsDataService();
		FinanceTrackingDataService financeService = new FinanceTrackingDataService();
		
		try	{
			incomeService.openPersistenceManager();
			financeService.openPersistenceManager();
			return calculateAverageMonthlyProfit(employed, incomeService, financeService, new Date());
		} catch (Exception e)	{
			return null;
		} finally {
			incomeService.closePersistenceManager();
			financeService.closePersistenceManager();
		}
	}
	@Deprecated
	public Double calculateAverageMonthlyProfit(boolean employed, IncomeSettingsDataService incomeService, 
			FinanceTrackingDataService financeService, Date now)	{
		try {
			Double income = Double.parseDouble(incomeService.retrieveIncome());
			Double fixedOutgoing = Double.parseDouble(incomeService.retrieveOutgoing());
			Double severableOutgoing = Double.parseDouble(incomeService.retrieveSeverableOutgoing());
			Double monthsTotalExpenses;
				monthsTotalExpenses = financeService.retrieveForecastSpendThisMonth(now, false);
			Double remaining = (employed?income:0.0) - fixedOutgoing - 
									(employed?severableOutgoing:0.0) - monthsTotalExpenses;
			return remaining;
		} catch (SecurityAccessDeniedException e) {
			return null;
		}
	}
	@Override
	public List<AssetDTO> retrieveAssets() throws IllegalArgumentException {
		AssetTrackingDataService dataService = new AssetTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			List<AssetDTO> assets = dataService.retrieveAssets();
			return assets;
		} catch (Exception e)	{
			log.severe("Update was unsuccessful for addAsset. " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}
	@Override
	public String updateAsset(AssetDTO dto)	throws IllegalArgumentException {
		AssetTrackingDataService dataService = new AssetTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			String returnString = dataService.updateAsset(dto);
			return returnString;
		} catch (Exception e)	{
			log.severe("Update was unsuccessful for addAsset. " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}
	@Override
	public List<FinanceGoalDTO> retrieveGoals() throws IllegalArgumentException {
		AssetTrackingDataService dataService = new AssetTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			List<FinanceGoalDTO> goals = dataService.retrieveGoals();
			return goals;
		} catch (Exception e)	{
			log.severe("Update was unsuccessful for retrieve Goal. " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}
	@Override
	public Boolean insertGoals() throws IllegalArgumentException {
		AssetTrackingDataService dataService = new AssetTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			Boolean result = dataService.insertGoal("New Goal", 0.0, 0);
			return result;
		} catch (Exception e)	{
			log.severe("Update was unsuccessful for addGoal. " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}
	@Override
	public String deleteAsset(Long id) throws IllegalArgumentException {
		AssetTrackingDataService dataService = new AssetTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			String result = dataService.deleteAsset(id);
			return result;
		} catch (Exception e)	{
			log.severe("Update was unsuccessful for addGoal. " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}
}
